package com.xli.mis.tablestruct.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xli.dto.component.model.TableModel;
import com.xli.dto.controller.BaseController;
import com.xli.dto.params.IdsDTO;
import com.xli.dto.result.ResultVO;
import com.xli.dto.result.status.Status;
import com.xli.dto.validation.group.IGroup;
import com.xli.mis.codemain.service.ICodeMainService;
import com.xli.mis.tablebasic.entity.TableBasic;
import com.xli.mis.tablebasic.service.ITableBasicService;
import com.xli.mis.tablestruct.entity.TableStruct;
import com.xli.mis.tablestruct.entity.dto.TableStructAddDTO;
import com.xli.mis.tablestruct.entity.dto.TableStructSearchDTO;
import com.xli.mis.tablestruct.entity.dto.TableStructUpdateDTO;
import com.xli.mis.tablestruct.entity.mapper.ITableStructMapper;
import com.xli.mis.tablestruct.entity.vo.TableStructVO;
import com.xli.mis.tablestruct.service.ITableStructService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Tag(name = "字段信息")
@RestController
@RequestMapping("/tableStruct")
public class TableStructController extends BaseController {

    @Autowired
    ICodeMainService iCodeMainService;

    @Autowired
    ITableBasicService iTableBasicService;

    @Autowired
    ITableStructService iTableStructService;

    @Operation(summary = "保存字段", description = "保存字段")
    @PostMapping(value = "/add")
    public ResultVO<String> add(@RequestBody @Validated(IGroup.add.class) TableStructAddDTO dto) {
        TableBasic tableBasic = iTableBasicService.find(Long.valueOf(dto.getTableId()));
        if (tableBasic != null) {
            TableStruct tableStruct = iTableStructService.findByName(tableBasic.getId(), dto.getFieldNameEn());
            if (tableStruct == null) {
                tableStruct = ITableStructMapper.INSTANCE.toEntity(dto);
                tableStruct.setId(IdUtil.getSnowflakeNextId());
                tableStruct.setSql_table_name(tableBasic.getSql_table_name());
                if ("NVARCHAR".equals(tableStruct.getField_type()) && (tableStruct.getField_length() == null || tableStruct.getField_length() <= 0)) {
                    tableStruct.setField_length(50);
                }
                boolean result = iTableStructService.insert(tableStruct);
                if (result) {
                    return new ResultVO<>(Status.SUCCESS, "添加成功");
                }
            }
            return new ResultVO<>(Status.FAILED, "保存失败，字段名已存在");
        }
        return new ResultVO<>(Status.FAILED, "保存失败，物理表不存在");
    }

    @Operation(summary = "删除字段", description = "删除字段")
    @PostMapping(value = "/delete")
    public ResultVO<String> delete(@RequestBody @Validated(IGroup.delete.class) IdsDTO dto) {
        int i = 0;
        for (String id : dto.getIds()) {
            if (iTableStructService.delete(Long.valueOf(id), true)) {
                i++;
            }
        }
        return new ResultVO<>(Status.SUCCESS, i + "条数据删除成功");
    }

    @Operation(summary = "更新字段", description = "更新字段")
    @PostMapping(value = "/update")
    public ResultVO<String> update(@RequestBody @Validated(IGroup.update.class) TableStructUpdateDTO dto) {
        TableStruct tableStruct = iTableStructService.findByName(Long.valueOf(dto.getTableId()), dto.getFieldNameEn());
        if (tableStruct == null || dto.getId().equals(tableStruct.getId().toString())) {
            if (iTableStructService.update(ITableStructMapper.INSTANCE.toEntity(dto))) {
                return new ResultVO<>(Status.SUCCESS, "更新成功");
            }
            return new ResultVO<>(Status.FAILED, "更新失败，字段名已存在");
        }
        return new ResultVO<>(Status.FAILED, "更新失败");
    }

    @Operation(summary = "查询字段列表", description = "查询字段列表")
    @PostMapping(value = "/search")
    public ResultVO<TableModel<TableStructVO, TableStruct, TableStructSearchDTO>> search(@RequestBody @Validated(IGroup.search.class) TableStructSearchDTO dto) {

        TableModel<TableStructVO, TableStruct, TableStructSearchDTO> tableModel = new TableModel<>(dto) {
            @Override
            public List<TableStructVO> fetch(int current, int pageSize) {
                Page<TableStruct> page = iTableStructService.findList(getQueryWrapper(), current, pageSize);
                this.setTotal(page.getTotal());
                return page.convert(ITableStructMapper.INSTANCE::toVO).getRecords();
            }
        };
        return new ResultVO<>(Status.SUCCESS, "查询成功", tableModel);
    }
}
